/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.ondoc.controller;

import com.etop.ondoc.beans.Category;
import com.etop.ondoc.beans.Document;
import com.etop.ondoc.dao.BDocDao;
import com.etop.ondoc.dao.BUserDao;
import com.etop.ondoc.dao.CategoryDao;
import com.etop.ondoc.dao.DaoFactory;
import com.etop.ondoc.dao.DocumentDao;
import com.etop.ondoc.util.DefaultValue;
import com.etop.ondoc.util.OndocException;
import com.etop.ondoc.util.Pager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author jansing lz_jansing@163.com
 */
@Controller
public class VisitorController {
    private DocumentDao docuDao = DaoFactory.getDocumentDao();
    private CategoryDao cateDao = DaoFactory.getCategoryDao();
    private BDocDao bdd = BDocDao.getInstance();
    private BUserDao bud = BUserDao.getInstance();
    int count = 1;

    /**
     * 主页的唯一入口
     * @return 
     */
    @RequestMapping(value = {"/main", "/"})
    public String main(Model model){
        List<Document> l = null;
        bdd = BDocDao.getInstance();
        try {
            l = (List<Document>) bdd.getHot();

        } catch (Exception e) {
            throw new OndocException("获取热门推荐失败！");
        }
        model.addAttribute("HotList", l);
        model.addAttribute("rootCategory", DefaultValue.ROOT_CATEGORY);
        return "main";
    }
    /**
     * 显示所有文档页面的唯一入口
     */
    @RequestMapping(value = "allFile", method = RequestMethod.GET)
    public String allFile(HttpServletRequest req, Model model) {
        Category cate;
                
        try {
            cate = cateDao.load(Integer.parseInt(req.getParameter("cateId")));
        } catch (NumberFormatException e) {
            cate = DefaultValue.ROOT_CATEGORY;
        }
        int pagesize = DefaultValue.PAGE_SIZE;
        int offset = 0;
        try {
            offset = Integer.parseInt(req.getParameter("pager.offset"));
        } catch (NumberFormatException e) {
        }
        Pager<Document> pager = docuDao.pager(offset, pagesize, cate);
        model.addAttribute("rootCategory", DefaultValue.ROOT_CATEGORY);
        model.addAttribute("pager", pager);
        //pager标签库需要的地址栏的条件
        model.addAttribute("cateId", cate.getId());
        return "/visitor/allFile";
    }

    /**
     * 在线浏览页面的唯一入口
     *
     * @return
     */
    @RequestMapping(value = "/onlineBrowse/{docuId}", method = RequestMethod.GET)
    public String onlineBrowse(@PathVariable int docuId, Model model) {
        Document docu = docuDao.load(docuId);
        model.addAttribute("docu", docu);
        return "/visitor/onlineBrowse";
    }

    /**
     * 搜索文档页面的唯一入口
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search(HttpServletRequest req, Model model) {
        int offset = 0;
        try {
            offset = Integer.parseInt(req.getParameter("pager.offset"));
        } catch (Exception e) {
        }
        int pagesize = 10;

        String hql = bdd.getHql(req);

        Pager<Document> pagers = bdd.getSearchDoc(req, hql, offset, pagesize);

        model.addAttribute("docuList", pagers);
        return "search";
    }
    
    /**
     * 通过ajax验证用户是否存在
     * @return 
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map checkUsername(HttpServletResponse resp, String username){
        boolean isUserExist = true;
        if(username==null||"".equals(username.trim())){
            isUserExist = false;
        }else{
            if(!bud.Checklogin(username, null)){
                isUserExist = false;
            }
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("isUserExist", isUserExist);
        return dataMap;
    }
    
    /**
     * 通过ajax验证登陆账号密码是否匹配
     * @return 
     */
    @RequestMapping("/checkPassword")
    @ResponseBody
    public Map checkPassword(HttpServletResponse resp, String username, String password){
        boolean isUserExist = true;
        if(username==null||"".equals(username)||password==null||"".equals(password.trim())){
            isUserExist = false;
        }else{
            if(!bud.Checklogin(username, password)){
                isUserExist = false;
            }
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("isUserExist", isUserExist);
        return dataMap;
    }
}
